//
//  SystemInboxCell.swift
//  WalkNovel
//
//  Created by Kevin on 2023/8/31.
//

import UIKit

class SystemInboxCell: UITableViewCell {
    
    lazy var timeBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitleColor(.init(named: "black_222222"), for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 12)
        return btn
    }()
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = .init(named: "white_F7F7F7")
        view.wn_cornerWithBorder(8)
        return view
    }()

    lazy var systemImage: UIImageView = {
        let image = UIImageView()
        image.contentMode = .scaleAspectFill
        image.wn_cornerWithBorder(8)
        return image
    }()

    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = .init(named: "black_333333")
        label.font = .wn_font(name: .bold, size: 15)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var conLabel: UILabel = {
        let label = UILabel()
        label.textColor = .init(named: "gray_666666")
        label.font = .wn_font(name: .regular, size: 13)
        label.numberOfLines = 0
        return label
    }()
    
    var model: MessageModel? {
        didSet {
            if let m = model {
                timeBtn.setTitle((m.ctime)?.wn_toJavaDetailDate(), for: .normal)
                let noImage = (m.imgUrl == nil || m.imgUrl?.count == 0)
                titleLabel.text = m.title
                conLabel.text = m.content
                if noImage {
                    systemImage.image = nil
                }else {
                    systemImage.wn_setupImg(m.imgUrl, placeHolder: "inbox_sys_image")
                }
                systemImage.snp.updateConstraints { (make) in
                    make.top.equalTo(noImage ? 0 : 20)
                    make.height.equalTo(noImage ? 0 : scaleWidth(ScreenW-80)*0.302)
                }
                layoutIfNeeded()
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.addSubview(timeBtn)
        contentView.addSubview(bgView)
        bgView.addSubview(systemImage)
        bgView.addSubview(titleLabel)
        bgView.addSubview(conLabel)
        timeBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(12)
        }
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(timeBtn.snp.bottom).offset(12)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(0)
        }
        systemImage.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(scaleWidth(ScreenW-80)*0.302)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(systemImage.snp.bottom).offset(12)
            make.left.equalTo(20)
            make.right.equalTo(-20)
        }
        conLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(6)
            make.left.equalTo(20)
            make.right.equalTo(-20)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
